package com.ruoyi.wms.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.common.core.annotation.Excel;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.utils.MD5Utils;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.wms.domain.Process;
import com.ruoyi.wms.dto.DisassembleTaskDto;
import com.ruoyi.wms.dto.ProcessDto;
import com.ruoyi.wms.mapper.ProcessMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.wms.mapper.DisassembleTaskMapper;
import com.ruoyi.wms.domain.DisassembleTask;
import com.ruoyi.wms.service.IDisassembleTaskService;

/**
 * 拆解任务Service业务层处理
 *
 * @author ruoyi
 * @date 2025-10-21
 */
@Service
public class DisassembleTaskServiceImpl implements IDisassembleTaskService {
    @Autowired
    private DisassembleTaskMapper disassembleTaskMapper;
    @Autowired
    private ProcessMapper processMapper;
    /**
     * 查询拆解任务
     *
     * @param id 拆解任务主键
     * @return 拆解任务
     */
    @Override
    public DisassembleTask selectDisassembleTaskById(String id) {
        return disassembleTaskMapper.selectDisassembleTaskById(id);
    }

    /**
     * 查询拆解任务列表
     *
     * @param disassembleTask 拆解任务
     * @return 拆解任务
     */
    @Override
    public List<DisassembleTask> selectDisassembleTaskList(DisassembleTaskDto disassembleTask) {
        return disassembleTaskMapper.selectDisassembleTaskList(disassembleTask);
    }

    /**
     * 新增拆解任务
     *
     * @param disassembleTask 拆解任务
     * @return 结果
     */
    @Override
    public int insertDisassembleTask(DisassembleTask disassembleTask) {

        disassembleTask.setId(MD5Utils.md5( disassembleTask.getPlateNo() +disassembleTask.getProcessName() ));
        disassembleTask.setCreateUser(SecurityUtils.getUsername());
        disassembleTask.setCreateDate(DateUtils.getCurrentTime());
        disassembleTask.setUpdateDate( DateUtils.getCurrentTime());
        return disassembleTaskMapper.insertDisassembleTask(disassembleTask);
    }

    /**
     * 修改拆解任务
     *
     * @param disassembleTask 拆解任务
     * @return 结果
     */
    @Override
    public int updateDisassembleTask(DisassembleTask disassembleTask) {

        int updateResult = 0;
        if (StringUtils.isEmpty(disassembleTask.getId())) {
            updateResult = insertDisassembleTask(disassembleTask);
        }
        disassembleTask.setUpdateDate( DateUtils.getCurrentTime());
        updateResult = disassembleTaskMapper.updateDisassembleTask(disassembleTask);
        if (0 == updateResult) {
            updateResult = insertDisassembleTask(disassembleTask);
        }


        return updateResult;


    }

    /**
     * 批量删除拆解任务
     *
     * @param ids 需要删除的拆解任务主键
     * @return 结果
     */
    @Override
    public int deleteDisassembleTaskByIds(String[] ids) {
        return disassembleTaskMapper.deleteDisassembleTaskByIds(ids);
    }

    /**
     * 删除拆解任务信息
     *
     * @param id 拆解任务主键
     * @return 结果
     */
    @Override
    public int deleteDisassembleTaskById(String id) {
        return disassembleTaskMapper.deleteDisassembleTaskById(id);
    }

    @Override
    public String  assignTask(DisassembleTask task) {
        List<DisassembleTask > taskList =new ArrayList<>();
        List<Process> processeList = processMapper.selectProcessList(new ProcessDto());
        for(Process p :processeList){
            DisassembleTask  po =new DisassembleTask();

            BeanUtils.copyProperties(p,  po);

            po.setId(MD5Utils.md5( po.getPlateNo() +po.getProcessName() ));
            po.setCreateUser(SecurityUtils.getUsername());
            po.setCreateDate(DateUtils.getCurrentTime());
            po.setUpdateDate( DateUtils.getCurrentTime());
            if(StringUtils.isNotEmpty(p.getdismantler() )){
                po.setDismantler(  String.join(",", p.getdismantlerArr()  ));

            }else{
               throw new RuntimeException( "工序拆解员不能为空");
            }
            po.setTaskIssuer( task.getTaskIssuer());
            po.setPlateNo(task.getPlateNo());
            po.setUnit( p.getUnit());
            po.setProcessId(p.getId());
            po.setProcessName( p.getProcessName());
            po.setProductName(p.getDismantlingMaterials() );

            taskList.add(po );
        }
        int i = disassembleTaskMapper.batchSave(taskList);
//        刷新图片

        i =disassembleTaskMapper.syncProductImg();
        return i+"";
    }
}
